//
//  ChooseLogistCompanyViewController.m
//  yigou
//
//  Created by 秦乾龙 on 17/2/24.
//  Copyright © 2017年 hanyiwang. All rights reserved.
//

#import "ChooseLogistCompanyViewController.h"
#import "AFNetworking.h"//主要用于网络请求方法
#import "UIKit+AFNetworking.h"//里面有异步加载图片的方法
#import "AFHTTPSessionManager.h"
#import "UIButton+WebCache.h"
#import "UIImageView+WebCache.h"
#import "NavView.h"
#import "User.h"
#import "ZYTableViewCell.h"
#import "FillLogistViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
#define kScale   ([UIScreen mainScreen].bounds.size.width)/750
@interface ChooseLogistCompanyViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *mainTableView;
@property (nonatomic,retain) NavView *nav;
@property (nonatomic,retain) NSArray *companyArr;
@property (nonatomic,retain) NSMutableArray *titleArr;
@property (nonatomic,retain) NSMutableArray *idArr;
@property(nonatomic,retain)User *user;


@end

@implementation ChooseLogistCompanyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.titleView = [ViewFactory makeNavigationTitle:@"选择物流公司"];
    
    UIBarButtonItem *backBtn = [ViewFactory makeNavigationBackWhite:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    self.view.backgroundColor = [UIColor colorWithRed:32/255.0 green:32/255.0 blue:32/255.0 alpha:1.0f];
    _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenW , kScreenH-64) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _titleArr = [NSMutableArray array];
    [self.view addSubview:_mainTableView];
    
}

- (void)request
{
    NSString *urlStr = [NSString stringWithFormat:@"%@/order/wuliuList",APIYIGOU]
    ;
    DYBaseRequest *request = [DYBaseRequest new];
    request.requestUrl = urlStr;
    [request dyBgStartWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSDictionary *allDic = [NSDictionary changeType:request.responseJSONObject];
        NSLog(@"111%@",allDic);
//        _user = [[User alloc]initWithDic:allDic[@"data"]];
        _companyArr = allDic[@"data"];
        for (int i = 0; i <_companyArr.count; i++)
        {
            NSString *titleStr = _companyArr[i][@"value"];
            NSString *idStr = _companyArr[i][@"id"];
            [_titleArr addObject:titleStr];
            [_idArr addObject:idStr];
        }
        [_mainTableView reloadData];
    } error:^(YTKBaseRequest *request) {
        
    } failure:^(YTKBaseRequest *request) {
        [ViewFactory showErrorStrMsg:NETWORK_ERR];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _companyArr.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell =[[NSBundle mainBundle ]loadNibNamed:@"ZYTableViewCell" owner:nil options:nil][0];
    }
    
//    NSDictionary *dic = _titleArr[indexPath.row];
    
    UILabel *titleLable = [[UILabel alloc]init];
    titleLable.text = _titleArr[indexPath.row];
    CGSize titleSize = [titleLable sizeThatFits:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    titleLable.frame = CGRectMake(30*kScale, (100-44)*kScale/2, titleSize.width, 44*kScale);
    [cell addSubview:titleLable];
    
    //右侧小箭头
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  100*kScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.01;
    }
    return 20*kScale;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    FillLogistViewController *VC = [[FillLogistViewController alloc]init];
//    self.delegate = VC;
    [self.delegate returnName:_titleArr[indexPath.row]];
    [self.delegate returnId:_idArr[indexPath.row]];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)backClick
{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self request];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
